/**
 * Created by Ysssssss on 20/12/14.
 */

Component({
    properties: {
        propertyInfo: {
            type: Object,
            value: null,
            observer: function (newVal, oldVal) {
                oldVal || this.setData({
                    focus: true
                })
            },
        },
        propertyError: {
            type: Object,
            value: null,
        }
    },
    data: {
        focusSubIndex: '',
        focus: false
    },
    lifetimes: {
        ready() {
        },
    },
    methods: {
        deleteProperty(e) {
            const { propertyInfo } = this.data
            this.triggerEvent('deleteProperty', propertyInfo.index)
        },

        handlePropertyNameChange(e) {
            const { propertyInfo, propertyError } = this.data
            this.triggerEvent('propertyChange', {
                ...propertyInfo,
                key: e.detail
            })
            propertyError && this.setData({
                propertyError: {
                    ...propertyError,
                    keyValid: false,
                }
            })
        },

        handlePropertyNameFocus() {
            this.setData({
                focus: true
            })
        },

        handlePropertyNameBlur() {
            this.setData({
                focus: false
            })
        },

        handlePropertySubNameFocus(e) {
            const { index } = e.currentTarget.dataset
            this.setData({
                focusSubIndex: index
            })
        },

        handlePropertySubNameBlur() {
            this.setData({
                focusSubIndex: ''
            })
        },

        handlePropertySubNameChange(e) {
            const { propertyInfo, propertyError } = this.data
            const { index } = e.currentTarget.dataset
            this.triggerEvent('propertyChange', {
                ...propertyInfo,
                values: [
                    ...propertyInfo.values.slice(0, index),
                    e.detail,
                    ...propertyInfo.values.slice(index + 1)
                ]
            })
            propertyError && this.setData({
                propertyError: {
                    ...propertyError,
                    valuesValid: [
                        ...propertyError.valuesValid.slice(0, index),
                        false,
                        ...propertyError.valuesValid.slice(index + 1)
                    ],
                }
            })
        },

        deletePropertySub(e) {
            const { propertyInfo } = this.data
            const { index } = e.currentTarget.dataset
            this.triggerEvent('propertyChange', {
                ...propertyInfo,
                values: [
                    ...propertyInfo.values.slice(0, index),
                    ...propertyInfo.values.slice(index + 1)
                ]
            })
        },

        addPropertySub() {
            const { propertyInfo } = this.data
            this.triggerEvent('propertyChange', {
                ...propertyInfo,
                values: [
                    ...propertyInfo.values,
                    '',
                ]
            })
            this.setData({
                focusSubIndex: propertyInfo.values.length
            })
        }
    }
})